// DialogShuffledNetworks.cpp : implementation file
//

#include "stdafx.h"
#include "testSVG.h"
#include "testSVGDlg.h"
#include <fstream>
#include <iostream>
#include "DialogShuffledNetworks.h"
#include ".\dialogshufflednetworks.h"

using namespace std; 

extern MOLECULE* GetNodeBasedOnNumber(int i);
extern MOLECULE* MoleculesList;
extern int NumberOfMolecules;
extern int NumberOfInteractions;
extern INTERACTION* InteractionsList;
extern char OriginalNetworkName[500];
extern bool CancelShuffle;

extern int GetRandomNumber(int range);
extern int CountIslands(void);
extern int ThereIsNoLinkAlready(INTERACTION* interaction1, INTERACTION* interaction2);
//void GenerateMiloStyleShuffledNetworks(int n, bool island);
//void SwapLinkEffects(int n);
extern int LoadNetwork(char* fileName);
extern void FreeNetwork(void);
//void GenerateErdosRenyiNetworks(int n, bool island);
//void FreeNewInteractionsList(INTERACTION* start);
// CDialogShuffledNetworks dialog

BOOL CDialogShuffledNetworks::OnInitDialog()
{
	CButton* randomSwap;
	CButton* shuffle;
	CButton* randomNet;
	CButton* island;
	CButton* clustering;
	CancelShuffle=false;
	randomSwap = (CButton*) GetDlgItem(IDC_RADIO_SHUFFLE_LINKS);
	shuffle = (CButton*) GetDlgItem(IDC_RADIO_SHUFFLE_SIGN);
	randomNet = (CButton*) GetDlgItem(IDC_RADIO3);
	island = (CButton*) GetDlgItem(IDC_CHECK1);
	clustering = (CButton*) GetDlgItem(IDC_CHECK4);
	randomSwap->SetCheck(1);
	island->SetCheck(0);
	clustering->SetCheck(1);
	CEdit* numberSet;
	numberSet = (CEdit*) GetDlgItem(IDC_EDIT1);
	typeOfNetwork=0;
	numberOfNetworks=0;
	isIsland=false;
	cccpl=false;
	motif34=false;
	motif5=false;
	SetDlgItemInt(IDC_EDIT1,100);
	

	return true;

}
IMPLEMENT_DYNAMIC(CDialogShuffledNetworks, CDialog)
CDialogShuffledNetworks::CDialogShuffledNetworks(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogShuffledNetworks::IDD, pParent)
{
}

CDialogShuffledNetworks::~CDialogShuffledNetworks()
{
}

void CDialogShuffledNetworks::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDialogShuffledNetworks, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_GENERATE_NETWORKS, OnBnClickedButtonGenerateNetworks)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


// CDialogShuffledNetworks message handlers

void CDialogShuffledNetworks::OnBnClickedButtonGenerateNetworks()
{
	CButton* generate=(CButton*) GetDlgItem(IDC_BUTTON_GENERATE_NETWORKS);
	generate->EnableWindow(0);
	CButton* exit=(CButton*) GetDlgItem(IDOK);
	exit->EnableWindow(0);
	CButton* randomSwap;
	CButton* shuffle;
	CButton* randomNet;
	CButton* island;
	CButton* clustering;
	CButton* motif3check;
	CButton* motif5check;
	randomSwap = (CButton*) GetDlgItem(IDC_RADIO_SHUFFLE_LINKS);
	shuffle = (CButton*) GetDlgItem(IDC_RADIO_SHUFFLE_SIGN);
	randomNet = (CButton*) GetDlgItem(IDC_RADIO3);
	island = (CButton*) GetDlgItem(IDC_CHECK1);
	clustering = (CButton*) GetDlgItem(IDC_CHECK4);
	motif3check = (CButton*) GetDlgItem(IDC_CHECK2);
	motif5check = (CButton*) GetDlgItem(IDC_CHECK3);
	numberOfNetworks=GetDlgItemInt(IDC_EDIT1);
	if (island->GetCheck())
	{
		isIsland=true;
	}
	if (randomSwap->GetCheck())
	{
		typeOfNetwork=1;
		//GenerateMiloStyleShuffledNetworks(GetDlgItemInt(IDC_EDIT1),isIsland);
		//call RandomSwap Function
	}
	else if (shuffle->GetCheck())
	{
		typeOfNetwork=2;
		//SwapLinkEffects(GetDlgItemInt(IDC_EDIT1));
		//call Shuffle function
	}
	else if (randomNet->GetCheck())
	{
		typeOfNetwork=3;
		//GenerateErdosRenyiNetworks(GetDlgItemInt(IDC_EDIT1),isIsland);//call RandomNetwork function
	}
	
	if (clustering->GetCheck())
	{
		cccpl=true;
	}
	if (motif3check->GetCheck())
	{
		motif34=true;
	}
	if (motif5check->GetCheck())
	{
		motif5=true;
	}
	generate->EnableWindow(1);
	exit->EnableWindow(1);
	OnOK();

	// TODO: Add your control notification handler code here
}
/*
void GenerateMiloStyleShuffledNetworks(int n, bool island)
{
	INTERACTION* tempNode;
	INTERACTION* tempNode1;
	INTERACTION* tempNode2;
	ofstream out;
	int randomNumber;
	char fileName[100];
	char tempStr[100];
	int i, j;

	for (i = 1; i <= n;)
	{
		sprintf(fileName, "random_milo_%d.sig", i);

		out.open(fileName);

		tempNode = InteractionsList;

		tempNode1 = InteractionsList;

		while (tempNode1)
		{
			randomNumber = GetRandomNumber(NumberOfInteractions);

			tempNode2 = InteractionsList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode2 = tempNode2->next;
			}

			if ((tempNode1 != tempNode2) && (ThereIsNoLinkAlready(tempNode1, tempNode2)))
			{
				
				strcpy(tempStr, tempNode1->target);
				strcpy(tempNode1->target, tempNode2->target);
				strcpy(tempNode2->target, tempStr);

				strcpy(tempStr, tempNode1->targetAccHuman);
				strcpy(tempNode1->targetAccHuman, tempNode2->targetAccHuman);
				strcpy(tempNode2->targetAccHuman, tempStr);

				strcpy(tempStr, tempNode1->targetAccMouse);
				strcpy(tempNode1->targetAccMouse, tempNode2->targetAccMouse);
				strcpy(tempNode2->targetAccMouse, tempStr);

				strcpy(tempStr, tempNode1->targetLocation);
				strcpy(tempNode1->targetLocation, tempNode2->targetLocation);
				strcpy(tempNode2->targetLocation, tempStr);

				strcpy(tempStr, tempNode1->targetType);
				strcpy(tempNode1->targetType, tempNode2->targetType);
				strcpy(tempNode2->targetType, tempStr);
	
			}
			tempNode1 = tempNode1->next;
		}

		tempNode = InteractionsList;
		while (tempNode)
		{
			
			out << tempNode->source << " " << tempNode->sourceAccHuman << " " << tempNode->sourceAccMouse << " " << tempNode->sourceType << " " << tempNode->sourceLocation << " " << tempNode->target << " ";
			out << tempNode->targetAccHuman << " " << tempNode->targetAccMouse << " " << tempNode->targetType << " " << tempNode->targetLocation << " " << tempNode->effect << " " << tempNode->typeOfInteraction;
			out << " " << tempNode->pmid << endl;
			
			tempNode = tempNode->next;
		}

		out.close();

		if (island)
		{
			FreeNetwork();
			LoadNetwork(fileName);
			if (CountIslands()==1)
			{
				i++;
			
			}
			FreeNetwork();
			LoadNetwork(OriginalNetworkName);
		}
		else
		{
			i++;
		}
		
	}
	AfxMessageBox("Done Creating Networks");
}
void SwapLinkEffects(int n)
{
	INTERACTION* tempNode;
	INTERACTION* tempNode1;
	INTERACTION* tempNode2;
	ofstream out;
	int randomNumber;
	char fileName[100];
	char tempStr[100];
	int i, j;

	for (i = 1; i <= n;i++)
	{
		sprintf(fileName, "random_effect_%d.sig", i);

		out.open(fileName);
		for (int runs=0;runs<2;runs++)
		{
			tempNode = InteractionsList;

			tempNode1 = InteractionsList;

			while (tempNode1)
			{
				randomNumber = GetRandomNumber(NumberOfInteractions);

				tempNode2 = InteractionsList;

				for (j = 0; j < randomNumber; j++)
				{
					tempNode2 = tempNode2->next;
				}

				if (tempNode1 != tempNode2)
				{
					strcpy(tempStr, tempNode1->effect);
					strcpy(tempNode1->effect, tempNode2->effect);
					strcpy(tempNode2->effect, tempStr);
				}
				tempNode1 = tempNode1->next;
			}
		}
		tempNode = InteractionsList;
		while (tempNode)
		{
			
			out << tempNode->source << " " << tempNode->sourceAccHuman << " " << tempNode->sourceAccMouse << " " << tempNode->sourceType << " " << tempNode->sourceLocation << " " << tempNode->target << " ";
			out << tempNode->targetAccHuman << " " << tempNode->targetAccMouse << " " << tempNode->targetType << " " << tempNode->targetLocation << " " << tempNode->effect << " " << tempNode->typeOfInteraction;
			out << " " << tempNode->pmid << endl;
			
			tempNode = tempNode->next;
		}

		out.close();		
	}
	AfxMessageBox("Done Creating Networks");

}
void GenerateErdosRenyiNetworks(int n, bool island)
{
	INTERACTION* tempNode;
	MOLECULE* tempNode1;
	MOLECULE* tempNode2;
	INTERACTION* start;
	INTERACTION* tempNode3;
	INTERACTION* tempNode4;
	ofstream out;
	int randomNumber;
	int numPositiveEffects=0;
	int numNegativeEffects=0;
	int numNeutralEffects=0;
	int totalEffects;
	char fileName[100];
	char tempStr[100];
	int i, j;

	for (i = 1; i <=n;)
	{
		numPositiveEffects=0;
		numNegativeEffects=0;
		numNeutralEffects=0;
		sprintf(fileName, "random_er_%d.sig", i);
		
		out.open(fileName);
		tempNode = InteractionsList;
		tempNode3 = new INTERACTION;

		while (tempNode)
		{
			if (strcmp(tempNode->effect,"+")==0) numPositiveEffects++;
			else if (strcmp(tempNode->effect,"_")==0) numNegativeEffects++;
			else if (strcmp(tempNode->effect,"0")==0) numNeutralEffects++;
			tempNode=tempNode->next;
		}
		totalEffects=numPositiveEffects+numNegativeEffects+numNeutralEffects;
		tempNode=InteractionsList;

		for (int h=0;h<numPositiveEffects;)
		{
			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode1 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode1 = tempNode1->next;
			}

			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode2 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode2 = tempNode2->next;
			}

			//create interaction

			if (tempNode1 != tempNode2)
			{
				strcpy(tempNode3->source,tempNode1->name);
				strcpy(tempNode3->sourceAccHuman,tempNode1->accHuman);
				strcpy(tempNode3->sourceAccMouse,tempNode1->accMouse);
				strcpy(tempNode3->sourceLocation,tempNode1->location);
				strcpy(tempNode3->sourceType,tempNode1->moleculeType);
				strcpy(tempNode3->target,tempNode2->name);
				strcpy(tempNode3->targetAccHuman,tempNode2->accHuman);
				strcpy(tempNode3->targetAccMouse,tempNode2->accMouse);
				strcpy(tempNode3->targetLocation,tempNode2->location);
				strcpy(tempNode3->targetType,tempNode1->moleculeType);
				strcpy(tempNode3->effect,"+");
				strcpy(tempNode3->typeOfInteraction,"Unknown");
				tempNode3->pmid=0;
                
				if (h==0) start=tempNode3;
				tempNode3->next = new INTERACTION;
				tempNode3=tempNode3->next;
				h++;
			}

		}

		for (int k=0;k<numNegativeEffects;)
		{
			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode1 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode1 = tempNode1->next;
			}

			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode2 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode2 = tempNode2->next;
			}

			//create interaction

			if (tempNode1 != tempNode2)
			{
				strcpy(tempNode3->source,tempNode1->name);
				strcpy(tempNode3->sourceAccHuman,tempNode1->accHuman);
				strcpy(tempNode3->sourceAccMouse,tempNode1->accMouse);
				strcpy(tempNode3->sourceLocation,tempNode1->location);
				strcpy(tempNode3->sourceType,tempNode1->moleculeType);
				strcpy(tempNode3->target,tempNode2->name);
				strcpy(tempNode3->targetAccHuman,tempNode2->accHuman);
				strcpy(tempNode3->targetAccMouse,tempNode2->accMouse);
				strcpy(tempNode3->targetLocation,tempNode2->location);
				strcpy(tempNode3->targetType,tempNode1->moleculeType);
				strcpy(tempNode3->effect,"_");
				strcpy(tempNode3->typeOfInteraction,"Unknown");
				tempNode3->pmid=0;
                
				tempNode3->next = new INTERACTION;
				tempNode3=tempNode3->next;
				k++;
			}

		}

		for (int l=0;l<numNeutralEffects;)
		{
			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode1 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode1 = tempNode1->next;
			}

			//get random molecule 1

			randomNumber = GetRandomNumber(NumberOfMolecules);

			tempNode2 = MoleculesList;

			for (j = 0; j < randomNumber; j++)
			{
				tempNode2 = tempNode2->next;
			}

			//create interaction

			if (tempNode1 != tempNode2)
			{
				strcpy(tempNode3->source,tempNode1->name);
				strcpy(tempNode3->sourceAccHuman,tempNode1->accHuman);
				strcpy(tempNode3->sourceAccMouse,tempNode1->accMouse);
				strcpy(tempNode3->sourceLocation,tempNode1->location);
				strcpy(tempNode3->sourceType,tempNode1->moleculeType);
				strcpy(tempNode3->target,tempNode2->name);
				strcpy(tempNode3->targetAccHuman,tempNode2->accHuman);
				strcpy(tempNode3->targetAccMouse,tempNode2->accMouse);
				strcpy(tempNode3->targetLocation,tempNode2->location);
				strcpy(tempNode3->targetType,tempNode1->moleculeType);
				strcpy(tempNode3->effect,"0");
				strcpy(tempNode3->typeOfInteraction,"Unknown");
				tempNode3->pmid=0;
                
				tempNode3->next = new INTERACTION;
				tempNode3=tempNode3->next;
				l++;
			}
		}
		tempNode4 = start;
		for (int te=0;te<totalEffects && (tempNode4);te++)
		{
			
			out << tempNode4->source << " " << tempNode4->sourceAccHuman << " " << tempNode4->sourceAccMouse << " " << tempNode4->sourceType << " " << tempNode4->sourceLocation << " " << tempNode4->target << " ";
			out << tempNode4->targetAccHuman << " " << tempNode4->targetAccMouse << " " << tempNode4->targetType << " " << tempNode4->targetLocation << " " << tempNode4->effect << " " << tempNode4->typeOfInteraction;
			out << " " << tempNode4->pmid << endl;
			
			tempNode4 = tempNode4->next;		
		}
	
		out.close();
		if (island)
		{
			FreeNetwork();
			LoadNetwork(fileName);
			if (CountIslands()==1)
			{
				i++;
			
			}
			FreeNetwork();
			LoadNetwork(OriginalNetworkName);
		}
		else
		{
			i++;
		}
		FreeNewInteractionsList(start);
	}
	AfxMessageBox("Done Creating Networks");
	
}
void FreeNewInteractionsList(INTERACTION* start)
{
	INTERACTION* tempNode;

	tempNode = start;

	while (start)
	{
		InteractionsList = tempNode->next;
		free(tempNode);
		tempNode = start;
	}

	NumberOfInteractions = 0;
	return;
	}*/
void CDialogShuffledNetworks::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	CancelShuffle=true;
	OnCancel();
}
